package javablackbelt.aspects;

import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;
import org.springframework.util.StopWatch;

@Component
@Aspect
public class PerformanceMonitor {

	protected final static Logger logger = Logger.getLogger(PerformanceMonitor.class);

	@Around("execution(* javablackbelt.beans.BankService.computeBalanceWithInterests(..))")
	public Object profile(ProceedingJoinPoint pjp) throws Throwable {
		
		StopWatch sw = new StopWatch(getClass().getSimpleName());
		
		try {
			sw.start(pjp.getSignature().getName());
			return pjp.proceed();
		}
		finally {
			sw.stop();
			logger.info(sw.prettyPrint());
		}
	}

}
